{thebe}
Unit 1: The Digital Lab Book#
Learning Outcomes#
By the end of this unit, students will be capable of
Identifying the main parts of a computational notebook and their functions (Remember)
Explaining how Python executes commands and displays output (Understand)
Performing basic arithmetic operations using Python (Apply)
Plotting a straight-line graph for a given linear equation and observing changes (Apply)
Activity 1.1: Meet Your Environment#
What is a “code editor” or “IDE”?#
A code editor is like a simple, blank piece of parchment (patra) and a pen (lekhani). It’s a clean place where you write your code, but that’s often all it does. Common examples are Notepad (on Windows) or Sublime Text.
An IDE (Integrated Development Environment) is like a complete scholar’s desk. It’s a magical setup that has:
The Editor: The same parchment and pen for writing.
A “Run” Button: A helper who can instantly read your code and show you the result.
A Guide (
shabdakosh): A helper who suggests the right words as you type (auto-completion).A Corrector (
vyakaran): A helper who puts a red line under any spelling or grammar mistakes in your code, helping you fix them (debugging).
Popular IDEs, especially for Python, include PyCharm Community, the beginner-friendly Thonny, or the very powerful Visual Studio Community.
In short, an editor is just for writing, while an IDE is a full workshop that helps you write, check, and run your code all in one place.
Writing and running your first command to see an output in the “Console” or “Terminal.”#
Think of yourself as a director 🎬 and the computer as an actor.
A command is an instruction you write in your script. The most common first command is print(). When you write print("Hello, world!"), you are writing a line in the script that tells your actor: “Say ‘Hello, world!’”
When you run the code, you are shouting “Action!” 🎬 The computer (your actor) reads that line of the script and performs the instruction.
The “Console” or “Terminal” is the stage. It’s the special output area where the actor delivers the line for you (the audience) to see. When you run your print command, the text “Hello, world!” appears in the console. It’s the computer’s way of showing you it has followed your command.
Your First Task:#
Run the code cell below. This is your first Kriya!
print("Hello, world! I am ready to analyze data.")
You should see the text Hello, world! I am ready to analyze data. appear in the output area (the “stage”). Congratulations, you have just given your first command to the computer!
Activity 1.2: Code as a Calculator#
In Python, you can perform calculations just like on a calculator. The computer understands these basic symbols:
+for Addition-for Subtraction*for Multiplication/for Division
In our last activity, we used print() to tell the computer (our “actor”) to say a sentence. We can also use it to tell the actor to “calculate this and say the answer.”
Task 1: Checking the Order of Operations#
Does the computer follow the rules of “BODMAS” (order of operations)? BODMAS rule tells you the sequence to follow: Brackets/Parentheses first, then Orders/Exponents, then Division/Multiplication, and finally Addition/Subtraction.
B - Brackets
O - Orders (or “Of,” meaning powers/exponents and roots)
D - Division
M - Multiplication
A - Addition
S - Subtraction
Let’s test it.
In your head, what is the answer to 10 + 5 * 2? Is it 30 or 20?
Run the code cell below to see how the computer solves it.
print(10 + 5 * 2)```
print(10 + 5 * 2)
Task 2: Your Turn#
Write the code in the cell below to solve and print the answer for this problem: (50 - 14) / (2 * 3)
# Type your code here to solve print(50 - 14) / (2 * 3)
Task 3: Dealing with Messy Numbers#
When you divide, you often get long, messy decimal numbers. Run the code cell below:
print(10 / 3)
You will see an output like 3.3333333333333335. This is not very readable.
We can tell the computer how to print the number. This is called formatting. We can tell our “actor” (the computer) not just what to say, but how to say it.
We use a special “f-string” for this. The .2f tells Python to “format this as a number with 2 decimal places.”
Run this code to see the difference:
# We save our messy number in a "variable"
my_number = 10 / 3
# Now we print it using formatting
print(f"The formatted answer is {my_number:.2f}")
Activity 1.3: Plotting Your First Graph#
Understanding the concept of a “library”#
Think of Python as a basic toolbox 🧰. It has all the essential tools like print() and basic math operators.
But what if you need a specialized tool, like a power drill for a big job? You wouldn’t build one from scratch; you’d get it from your workshop.
A library in Python is just like that. It’s a pre-built collection of advanced tools. For data analysis, we need tools to handle number lists (from numpy) and tools to draw graphs (from matplotlib).
How to import a library#
To use these tools, you must first bring them into your script. We use the import command, which is like saying, “Go to the workshop and get me this specific toolbox.”
# We import the 'matplotlib.pyplot' toolbox and give it a short nickname 'plt'
import matplotlib.pyplot as plt
# We import the 'numpy' toolbox and give it the nickname 'np'
import numpy as np
# We import the 'matplotlib.pyplot' toolbox and give it a short nickname 'plt'
import matplotlib.pyplot as plt
# We import the 'numpy' toolbox and give it the nickname 'np'
import numpy as np
Running a complete script to plot y = 2x + 1#
Now, let’s put it all together. The script below will:
Import the necessary libraries.
Define the x and y values for our equation.
Use the plt tools to draw the graph.
Use plt.show() to display the graph in a new window.
Task: Run the complete script below. You should see a new window pop up with your first graph.
import numpy as np
import matplotlib.pyplot as plt
# 1. Define x-values (100 points from -5 to 5)
x = np.linspace(-5, 5, 100)
# 2. Calculate y-values using our equation
y = 2*x + 1
# 3. Use the 'plt' library to set up the graph
plt.plot(x, y)
plt.title("Graph of y = 2x + 1")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.grid(True)
# 4. Show the final graph
plt.show()
What did np.linspace do?
The line x = np.linspace(-5, 5, 100) might look complex, but it’s very simple.
It’s a command from the numpy library (which we nicknamed np) that creates a list of evenly spaced numbers.
Think of it like this:
-5is the start point.5is the end point.100is the total number of points we want.
So, this command gives us 100 numbers perfectly spaced out between -5 and 5, which we use as the coordinates for our x-axis to draw a smooth line.
Animation 1.1#
To make this idea clearer, watch the animation below.
The animation shows the same mathematical function drawn using different numbers of points generated by np.linspace. When only a few points are used, the graph looks broken or rough. As more points are added, the computer joins them more closely, and the curve appears smoother.
This helps you see that smooth graphs are created by calculating many points and joining them, not by drawing curves directly.
Activity 1.4: Experimenting by Changing Code#
Now you will be a data scientist 🧑🔬. Your job is to experiment by changing the code you just ran.
This “manual edit and re-run” cycle is the core workflow for all IDEs. It’s a fundamental skill that lets you test ideas and see the results immediately.
Interactive Activity 1.1#
Before changing the code manually, let us first experiment in a simpler way.
In the interactive activity below, you can move sliders to change the values of m and c in the equation:
$\(y = mx + c\)$
As you move the sliders, the graph updates instantly.
This allows you to observe:
how changing
maffects the steepness of the linehow changing
cmoves the line up or down
Think of this as a quick visual experiment before you perform the same changes directly in the code.
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, FloatSlider
from IPython.display import display
def plot_line(m, c):
plt.figure(figsize=(8, 6))
x = np.linspace(-10, 10, 400)
y = m * x + c
plt.plot(x, y, color='blue')
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.grid(True)
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.title(f"Graph of y = {m}x + {c}")
plt.xlabel('x')
plt.ylabel('y')
plt.show()
m_slider = FloatSlider(min=-10, max=10, step=0.5, value=1, description='m (Slope):')
c_slider = FloatSlider(min=-10, max=10, step=1, value=0, description='c (Intercept):')
interact(plot_line, m=m_slider, c=c_slider);
Task 1: Change the Slope (m)#
Copy the complete code from Activity 1.3 into the empty cell below.
Find the line
y = 2*x + 1.Change it to
y = 5*x + 1.Re-run the entire script.
Observe the new graph: Did the line get flatter or steeper?
# Copy the code from Activity 1.3 here
# and make your first change
import numpy as np
import matplotlib.pyplot as plt
# 1. Define x-values
x = np.linspace(-5, 5, 100)
# 2. Calculate y-values (CHANGED)
y = 5*x + 1
# 3. Use the 'plt' library to set up the graph
plt.plot(x, y)
plt.title("Graph of y = 5x + 1")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.grid(True)
# 4. Show the final graph
plt.show()
Task 2: Change the Intercept (c)#
Go back to the code you just pasted (or copy the original code again).
This time, change the line y = 5x + 1 (or the original y = 2x + 1) to y = 2*x + 5.
Re-run the script.
Observe the change: Did the line’s steepness change, or did it move up or down?
# Copy the code again and make your second change
import numpy as np
import matplotlib.pyplot as plt
# 1. Define x-values
x = np.linspace(-5, 5, 100)
# 2. Calculate y-values (CHANGED)
y = 2*x + 5
# 3. Use the 'plt' library to set up the graph
plt.plot(x, y)
plt.title("Graph of y = 2x + 5")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.grid(True)
# 4. Show the final graph
plt.show()
Reflection#
Based on your experiments, which part of
y = mx + ccontrols the steepness?Which part controls where the line moves up or down?
How is this process of “changing a value and seeing the result” similar to a science experiment?